<template>
  <div id="app">
    <el-card shadow="always">
      {{skillSearch}}
      <el-form :inline="true" size="medium" :model="skillSearch" class="demo-form-inline">
        <el-form-item label="技能名">
          <el-input v-model="skillSearch.skillName" placeholder="技能名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-select v-model="skillSearch.characterId" placeholder="角色名" @change="onCategoryChange">
            <el-option label="所有角色" value="0"></el-option>
            <el-option :label="c.characterName" :value="c.characterId" v-for="c in characterList" :key="c.characterId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search" icon="el-icon-search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" plain @click="openDiag" icon="el-icon-plus">添加产品</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="danger" plain @click="handleDelete" icon="el-icon-remove">批量删除</el-button>
        </el-form-item>
      </el-form>
      <el-table
          :data="skillList"
          style="width: 100%"
          @selection-change="handleSelectionChange">
        <el-table-column
            type="selection"
            width="55">
        </el-table-column>
        <el-table-column
            label="技能名"
            prop="skillName"
            align="center">
        </el-table-column>
        <el-table-column
            label="技能数值"
            prop="skillValue"
            align="center">
        </el-table-column>
        <el-table-column
            label="技能等级"
            prop="skillLevel"
            align="center">
        </el-table-column>
        <el-table-column
            label="角色名"
            prop="characterName"
            align="center">
        </el-table-column>
        <el-table-column
            label="更新时间"
            prop="updateTime"
            align="center">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
                size="mini"
                @click="handleEdit(scope.row)">编辑</el-button>
            <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index,scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <span class="demonstration">完整功能</span>
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
        </el-pagination>
      </div>
    </el-card>
    <el-dialog title="title"  :close-on-click-modal="false" :visible.sync="dialogFormVisible">
      {{skill}}
      <el-form :model="skill" :rules="rules" ref="ruleForm">
        <el-form-item label="技能名" prop="skillName" :label-width="formLabelWidth">
          <el-input v-model="skill.skillName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="技能数值" prop="skillValue" :label-width="formLabelWidth">
          <el-input v-model="skill.skillValue" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="技能等级" prop="skillLevel" :label-width="formLabelWidth">
          <el-input v-model="skill.skillLevel" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色名" prop="characterId" :label-width="formLabelWidth">
          <el-select v-model="skill.characterId" placeholder="请选择角色">
            <el-option label="所有类别" value="0"></el-option>
            <el-option :label="c.characterName" :value="c.characterId" v-for="c in characterList" :key="c.characterId"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="selectType">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="handleClose">
      <span>你确定要删除技能吗？</span>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="open3">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>
<script>
import skillApi from "@/api/skill"
import characterApi from "@/api/character";

export default {
  computed: {
    category() {
      return category
    }
  },
  data() {
    return {
      skillList: [],
      characterList:[],
      page: 1,
      pageSize:10,
      total:100,
      skillSearch:{},//查询条件对象
      dialogFormVisible:false,  // 控制对话框的显示隐藏
      dialogVisible:false,
      formLabelWidth:'100px',   // 对话框提示文本的宽度
      skill: {},              // 用户对象（添加或修改）
      title: '添加技能',          // 对话框标题
      type: '添加',
      multipleSelection: [],
      deleteIds:[],
      ruleForm: {
        name: '',
        age:null,
        sex: '',
        phone: '',
      },
      rules: {
        skillName: [
          { required: true, message: '请输入技能名称', trigger: 'blur' },
          { min: 1, max: 100, message: '长度在 1 到 100 个字符', trigger: 'blur' }
        ],
        skillValue: [
          { required: true, message: '请输入技能数值', trigger: 'blur' },
        ],
        skillLevel: [
          {  required: true, message: '请输入技能等级', trigger: 'blur' }
        ]
      }
    }
  },
  created: function () {
    this.search()
    this.findCharacters()
  },
  methods: {
    //1.查询所有用户
    async findSkills(){
      const {data:res} = await skillApi.findAll()
      this.skillList =res.data.skills;
    },
    async findCharacters(){
      const {data:res} = await characterApi.findAllCharacter()
      this.characterList =res.data.list;
    },
    async findByPage(page, pageSize) {
      const {data:res} = await skillApi.findByPage(page,pageSize)
      if (res.code === 200){
        this.skillList =res.data.skills.rows;
        this.total = res.data.skills.total;
      }
    },
    open1() {
      this.saveSkills()
      this.$message({
        message: '添加技能成功',
        type: 'success'
      });
    },
    open2() {
      this.updateSkills()
      this.$message({
        message: '修改技能成功',
        type: 'success'
      });
    },
    open3() {
      this.deleteSkills()
      this.$message({
        message: '删除技能成功',
        type: 'warning'
      });
    },
    selectType(){
      if (this.type === '添加'){
        this.open1()
      }else if(this.type === '修改'){
        this.open2()
      }else if(this.type === '删除'){

      }
    },
    async search(){
      const {data:res} = await skillApi.search(this.page,this.pageSize,this.skillSearch)
      this.skillList = res.data.skills.rows
      this.total = res.data.skills.total
    },
    opend(){
      this.type = '删除'
      this.title = '删除技能'
      this.dialogVisible = true
    },
    openDiag(){
      this.type = '添加'
      this.title = '添加技能'
      this.skill = {}
      this.dialogFormVisible = true;
    },
    openDiagwithRow(row) {
      this.type = '修改'
      this.title = '修改技能'
      this.skill = { ...row };
      this.dialogFormVisible = true;
    },
    async saveSkills(){
      const {data:res} = await skillApi.save(this.skill)
      if (res.code === 200){
        // this.$message({
        //   type: 'success',
        //   message: res.message
        // })
        this.dialogFormVisible = false;
        this.search()
      }else {
        this.$message({
          type: 'error',
          message: res.message
        })
      }
    },
    async updateSkills() {
      const {data:res} = await skillApi.update(this.skill)
      if (res.code === 200){
        // this.$message({
        //   type: 'success',
        //   message: res.message
        // })
        this.dialogFormVisible = false;
        this.search()
      }else {
        this.$message({
          type: 'error',
          message: res.message
        })
      }
    },
    async deleteSkills(){
      const {data:res} = await skillApi.delete(this.deleteIds)
      if (res.code === 200){
        // this.$message({
        //   type: 'success',
        //   message: res.message
        // })
        this.dialogVisible = false;
        this.search()
      }else {
        this.$message({
          type: 'error',
          message: res.message
        })
      }
    },
    handleEdit(row) {

      this.openDiagwithRow(row)
      console.log(row);
    },
    handleDelete(index,row) {
      this.deleteIds = []
      if (row) {
        this.deleteIds.push(row.skillId)
        console.log("row.id", row.skillId);
      }else{
        this.deleteIds = this.multipleSelection.map(m => m.skillId)
      }
      this.opend()
    },
    handleSizeChange(val) {
      this.pageSize = val
      // this.findByPage()
      this.search()
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.page = val
      // this.findByPage()
      this.search()
      console.log(`当前页: ${val}`);
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    // 当选择类别时，确保页面重置为第一页
    onCategoryChange() {
      // 将当前页重置为第一页
      this.page = 1;
      this.search();  // 重新进行查询
    }
  }
}

</script>
<style>
.el-card {
  width: 90%;
  margin: auto;
}
#app{display: flex;
  justify-content: center;
}
.el-pagination{
  text-align: right;
  margin-top: 15px;
}
</style>
